package com.netprovider.user.service.baseinfo.impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;

import org.apache.cxf.jaxrs.ext.multipart.Multipart;

import com.netprovider.user.service.baseinfo.UserService;
import com.netprovider.user.service.baseinfo.dao.UserDAO;
import com.netprovider.user.service.baseinfo.objects.User;
import com.netprovider.user.service.baseinfo.objects.Users;

public class UserServiceImpl implements UserService {
	@POST
	@Consumes("multipart/form-data")
	public void upload(InputStream is) {
	}
	@POST
	@Consumes("multipart/form-data")
	public void upload1(@Multipart InputStream is) {
	}
	public User searchUser(String userId) {
		User user = UserDAO.searchUser(userId);
		if (user == null) {
			ResponseBuilder builder = Response.status(Status.NOT_FOUND);
			builder.type("application/xml");
			builder.entity("<errorMsg>User with id:" +
					       userId + " can not be found!</errorMsg>");
			throw new WebApplicationException(builder.build());
		} else {
			System.out.println("User with id:" + userId + " is found");
			return user;
		}

	}
	
	@POST
   @Path("/form1")
   @Consumes("application/x-www-form-urlencoded")
   public void form1(@FormParam("name") String name, @FormParam("age") int age) {
   }
 
   @POST
   @Path("/form2")
   @Consumes("application/x-www-form-urlencoded")
   public void form1(MultivaluedMap<String, String> params) {
       String name = params.getFirst("name");
       String age = params.getFirst("age");
   }
 
	public Response addUser(User user) {
		System.out.println(this);
		User userObj = (User) UserDAO.searchUser(user.getUserId());
		if (userObj != null) {
			ResponseBuilder builder = Response.status(Status.FORBIDDEN);
			builder.type("application/xml");
			builder.entity("<errorMsg>User with id:" + user.getUserId()  
					      + " already exists</errorMsg>");
			throw new WebApplicationException(builder.build());
		} else {
			UserDAO.addUser(user);
			return Response.ok(user).build();
		}
	}

	public Response deleteUser(String userId) {
		User userObj = (User) UserDAO.searchUser(userId);
		if (userObj == null) {
			ResponseBuilder builder = Response.status(Status.FORBIDDEN);
			builder.type("application/xml");
			builder.entity("<errorMsg>User with id:" + userId  
					      + " is not existed, delettion request is rejected</errorMsg>");
			throw new WebApplicationException(builder.build());
		} else {
			UserDAO.deleteUser(userId);
			return Response.ok().build();
		}
	}

	public Response updateUser(User user) {
		User userObj = (User) UserDAO.searchUser(user.getUserId());
		if (userObj == null) {
			ResponseBuilder builder = Response.status(Status.FORBIDDEN);
			builder.type("application/xml");
			builder.entity("<errorMsg>User with id:" + user.getUserId()  
					      + " is not existed, update request is rejected</errorMsg>");
			throw new WebApplicationException(builder.build());
		} else {
			UserDAO.updateUser(user);
			return Response.ok(user).build();
		}
	}

	public Users getUsers(String startDate, String endDate) {
		List<User> userList = new ArrayList<User>();
		ResponseBuilder builder = Response.status(Status.OK);
		builder.type("application/xml");
		try {
			userList = UserDAO.getUsersByRegDate(startDate, endDate);
		} catch (Exception e) {
			e.printStackTrace();
			builder = Response.status(Status.NOT_ACCEPTABLE);
			builder.entity("<errorMsg>" +
					"not accpertable date format for startDate or endDate</errorMsg>"); 
			throw new WebApplicationException(builder.build());
		}
		if (userList.size() < 1) {
			builder = Response.status(Status.NOT_FOUND);
			builder.entity("<errorMsg>no user found registered between" 
					 +startDate+ " and " + endDate + "</errorMsg>");
			throw new WebApplicationException(builder.build());
		} else {
			Users users = new Users();
			users.setUsers(userList);
			return users;
		}
	}
}
